package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.LunarUtil;

import java.util.ArrayList;
import java.util.List;

public class DaYun {
    private int startYear;
    private int endYear;
    private int startAge;
    private int endAge;
    private int index;
    private Yun yun;
    private Lunar lunar;

    public DaYun(Yun yun, int index) {
        this.yun = yun;
        this.lunar = yun.getLunar();
        this.index = index;
        int birthYear = this.lunar.getSolar().getYear();
        int year = yun.getStartSolar().getYear();
        if (index < 1) {
            this.startYear = birthYear;
            this.startAge = 1;
            this.endYear = year - 1;
            this.endAge = year - birthYear;
        } else {
            int add = (index - 1) * 10;
            this.startYear = year + add;
            this.startAge = this.startYear - birthYear + 1;
            this.endYear = this.startYear + 9;
            this.endAge = this.startAge + 9;
        }
    }
    

    public int getStartYear()
    {
        return this.startYear;
    }

    public int getEndYear()
    {
        return this.endYear;
    }

    public int getStartAge()
    {
        return this.startAge;
    }

    public int getEndAge()
    {
        return this.endAge;
    }

    public int getIndex()
    {
        return this.index;
    }

    public List<DaYun> getYun()
    {
        return this.yun.getDaYun();
    }

    public Lunar getLunar()
    {
        return this.lunar;
    }

    public String getGanZhi()
    {
        if (this.index < 1) {
            return "";
        }
        int offset = LunarUtil.getJiaZiIndex(this.lunar.getMonthInGanZhiExact());
        offset += this.yun.isForward() ? this.index : -this.index;
        int size = LunarUtil.JIA_ZI.size();
        if (offset >= size) {
            offset -= size;
        }
        if (offset < 0) {
            offset += size;
        }
        return LunarUtil.JIA_ZI.get(offset);
    }

    public String getXun()
    {
        return LunarUtil.getXun(this.getGanZhi());
    }

    public String getXunKong()
    {
        return LunarUtil.getXunKong(this.getGanZhi());
    }

    public List<LiuNian> getLiuNian()
    {
        return this.getLiuNianBy(10);
    }

    public List<LiuNian> getLiuNianBy(int n)
    {
        if (this.index < 1) {
            n = this.endYear - this.startYear + 1;
        }
        List<LiuNian> l = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            l.add(new LiuNian(this, i));
        }
        return l;
    }

    public List<XiaoYun> getXiaoYun()
    {
        return this.getXiaoYunBy(10);
    }

    public List<XiaoYun> getXiaoYunBy(int n)
    {
        if (this.index < 1) {
            n = this.endYear - this.startYear + 1;
        }
        List<XiaoYun> l = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            l.add(new XiaoYun(this, i, this.yun.isForward()));
        }
        return l;
    }
}
